/**
 * 格式化时间为指定格式的字符串
 * @param date - 时间对象或时间戳
 * @param format - 格式字符串，例如 "YYYY-MM-DD HH:mm:ss"
 * @returns 格式化后的字符串
 */
export const formatDate = function (date: Date | number = new Date(), format: string = 'YYYY-MM-DD'): string {
    const d = date instanceof Date ? date : new Date(date);

    const year = d.getFullYear() + '';
    const month = String(d.getMonth() + 1).padStart(2, '0'); // 月份从0开始
    const day = String(d.getDate()).padStart(2, '0');
    const hours = String(d.getHours()).padStart(2, '0');
    const minutes = String(d.getMinutes()).padStart(2, '0');
    const seconds = String(d.getSeconds()).padStart(2, '0');

    return format
        .replace("YYYY", year)
        .replace("MM", month)
        .replace("DD", day)
        .replace("HH", hours)
        .replace("mm", minutes)
        .replace("ss", seconds);
}
